package com.ktjiaoyu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktjiaoyu.mapper.NewsMapper;
import com.ktjiaoyu.pojo.News;
import com.ktjiaoyu.service.NewsService;
import com.ktjiaoyu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-05-29
 * @Description:
 * @Version: 1.0
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public R SelectNewsByPage(String title, String author, Integer typeId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<News> news = newsMapper.SelectNewsByPage(title, author, typeId);
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return R.ok(pageInfo);

    }

    @Override
    public R deleteNewsById(Integer id) {
        int i = newsMapper.deleteNewsById(id);
        if (i > 0) {
            return R.ok();
        } else {
            return R.fail("删除失败!");
        }
    }

    @Override
    public R deleteNewsByIds(List<Integer> ids) {
        int i = newsMapper.deleteNewsByIds(ids);
        if (i > 0) {
            return R.ok();
        } else {
            return R.fail("批量删除失败!");
        }
    }

    @Override
    public R saveNews(News news) {
        int i = 0;
        if (news.getId() == null) {
            i = newsMapper.insertNews(news);
        } else {
            i = newsMapper.updateNews(news);
        }
        if (i > 0) {
            return R.ok();
        }
        return R.fail("保存失败");
    }
}
